<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Post;
use App\Zan;
use Illuminate\Http\Request;
/*
 * 文章管理
 * */
class PostController extends Controller
{
    //文章列表
    public function index(){
        /*分页*/
        // withCount  计算关联一对多的表所查询出的数据总和，要获取该数据，用变量名_count,hasManyComments_count
        $posts = Post::orderBy('created_at','desc')->withCount(['zans','comments'])->paginate(6);
        return view('post.index',compact('posts'));
    }
    //文章详情
    public function detail(Post $post){
        $post->load('comments');
        return view('post.detail',compact('post'));
    }
    //文章新增入口
    public function create(){
        $user_id = \Auth::user()->id;
        if(empty($user_id)){
            return redirect()->route('login');
        }
        return view('post.create');
    }
    //文章新增
    public function save(){
        //验证
        $this->validate(request(),[
            'title'=>'required|string|max:100|min:5',
            'content'=>'required|string|min:10'
        ]);
        $user_id = \Auth::user()->id;
        $params = array_merge(request(['title','content']),compact('user_id'));
        $post = Post::create($params);
        return redirect('/posts');
    }
    //文章编辑
    public function edit(Post $post){
        //权限控制
        $this->authorize('edit',$post);
        return view('post.edit',compact('post'));
    }
    //文章更新
    public function update(Post $post){
        //验证
         $this->validate(request(),[
            'title'=>'required|string|max:100|min:5',
            'content'=>'required|string|min:10'
        ]);
        //权限控制
        $this->authorize('update',$post);
        $post->update(request(['title', 'content']));
        return redirect("/posts/d/{$post->id}");
    }
    //文章删除
    public function delete($id){
        $post = Post::find($id);
        //权限控制
        $this->authorize('delete',$post);
        if($post == null){
            return redirect()->back()->with('error','删除失败');
        }
        if($post->delete()){
            return redirect('/posts');
        }else{
            return redirect()->back()->with('error','删除失败');
        }
    }

    //新增评论
    public function comment(){
        $this->validate(request(),[
            'post_id' => 'required|exists:posts,id',
            'comment'=>'required|min:3'
        ]);

        $user_id = \Auth::id();
        $comment = new Comment();
        $params = array_merge(
            request(['post_id', 'comment']),
            compact('user_id')
        );
        $comment->create($params);

        return back();
    }

    //点赞
    public function zan(Post $post){
        $params = [
            'user_id'=>\Auth::id(),
            'post_id' =>$post->id
        ];
        $zan = Zan::firstOrCreate($params);

        return back();
    }

    //取消点赞
    public function unzan(Post $post){
        $post->zan(\Auth::id())->delete();
        return back();
    }

    //图片上传
    public function imageUpload(Request $request){
        $path = $request->file('wangEditorH5File')->store(md5(time()));
        return asset($path);
    }
}
